<template>
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container">
      <div @mouseenter="changeIsShow(true)" @mouseleave="changeIsShow(false)">
        <h2 class="all">全部商品分类</h2>
        <div class="sort" v-show="isShow">
          <div class="all-sort-list2">
            <div
              class="item"
              v-for="category in categoryList"
              :key="category.categoryId"
              @mouseenter="throttleEnter"
              @mouseleave="throttleLeave"
              @click="gotoSearch"
            >
              <h3>
                <a
                  :data-category1Id="category.categoryId"
                  :data-categoryName="category.categoryName"
                  >{{ category.categoryName }}</a
                >
              </h3>
              <div class="item-list clearfix">
                <div class="subitem">
                  <dl
                    class="fore"
                    v-for="child in category.categoryChild"
                    :key="child.categoryId"
                  >
                    <dt>
                      <a
                        :data-category2Id="child.categoryId"
                        :data-categoryName="child.categoryName"
                        >{{ child.categoryName }}</a
                      >
                    </dt>
                    <dd>
                      <em
                        v-for="item in child.categoryChild"
                        :key="item.categoryId"
                      >
                        <a
                          :data-category3Id="item.categoryId"
                          :data-categoryName="item.categoryName"
                          >{{ item.categoryName }}</a
                        >
                      </em>
                    </dd>
                  </dl>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";

import _ from "lodash";

export default {
  name: "TypeNav",
  data() {
    return {
      /*  当给列表绑定事件时,我们需要确定并选中是列表中哪个DOM触发的事件,有两种方法
      1.使用e.target
      2.使用下标 :创建一个全局变量值为-1,当事件触发时,将全局变量的值改为当前Dom的下标,再判断如果全局变量的值和下标相等,那么就给他添加类名
       */
      isShow: false,
    };
  },
  created() {
    //throttle: lodash封装时是一个异步函数,当模板解析挂载完成后,throttle还没有返回函数,此时this.throttleEnter还没有值,调用会出现""throttleEnter不是一个方法""的报错,所以将throttle放在数据初始化完成后,模板解析前,此时的throttleEnter就有值了
    this.throttleEnter = _.throttle(this.mouseEnter, 50, {
      leading: true,
      trailing: true,
    });
    this.throttleLeave = _.throttle(this.mouseLeave, 12, {
      leading: true,
      trailing: true,
    });
  },
  mounted() {
   
    //在home路由中,navtype显示
    this.isHome();
  },
  computed: {
    ...mapState("home", ["categoryList"]),
  },
  methods: {
    // throttleEnter: _.throttle(
    //   function (e) {
    //     e.target.classList.remove("active");
    //   },
    //   10,
    //   {
    //     leading: true,
    //     trailing: true,
    //   }
    // ),

    mouseEnter(e) {
      if (!e) return;
      e.target.classList.add("active");
    },
    mouseLeave(e) {
      e.target.classList.remove("active");
    },
    gotoSearch(e) {
      const query = {};
      const { category1id, category2id, category3id, categoryname } =
        e.target.dataset;
      if (category1id) {
        query.category1Id = category1id;
      } else if (category2id) {
        query.category2Id = category2id;
      } else if (category3id) {
        query.category3Id = category3id;
      }
      query.categoryName = categoryname;
      if (e.target.nodeName === "A") {
        const params = this.$route.params
        this.$router.push({
          name: "search",
          query,
          params
        });
      }
    },
    isHome() {
      if (this.$route.name === "home") this.isShow = true;
    },
    changeIsShow(boolean) {
      if (this.$route.name === "home") return;
      this.isShow = boolean;
    },
  },
};
</script>

<style lang='less' scoped>
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          &.active {
            background-color: aqua;
            .item-list {
              display: block;
            }
          }
        }
      }
    }
  }
}
</style>